package com.bdqn.t308.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class UserServiceAspectLooger {

    private static final Logger logger = Logger.getLogger(UserServiceAspectLooger.class);


    //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
    //切点的集合，这个表达式所描述的是一个虚拟面（规则）
    //就是为了Annotation扫描时能够拿到注解中的内容
    @Pointcut("execution(* com.bdqn.t308.service..*(..))")
    public void aspect(){}

    /**
     * 前置增强
     * @param joinPoint
     */
    @Before("aspect()")
    public void before(JoinPoint joinPoint){
        logger.info("调用"+joinPoint.getTarget()+"的"+joinPoint.getSignature().getName());
    }


    /**
     * 异常抛出增强
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value = "aspect()",throwing = "ex")
    public void throwError(JoinPoint joinPoint,RuntimeException ex){
        logger.info("调用"+joinPoint.getTarget()+"的"+joinPoint.getSignature().getName()
                +"方法，发生异常："+ex);
    }
}
